package com.xgpro.pc.talentintroduction.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.service.BaseService;
import com.xgpro.domain.talentintroduction.entity.IntroduceJob;
import com.xgpro.domain.talentintroduction.entity.Recruitment;
import com.xgpro.domain.talentintroduction.vo.*;
import com.xgpro.mapper.talentintroduction.IntroduceJobMapper;
import com.xgpro.mapper.talentintroduction.RecruitmentMapper;
import com.xgpro.pc.talentintroduction.service.RecruitmentPCService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/18 20:09
 */
@Service
@Transactional(readOnly = false)
public class RecruitmentPCServiceImpl extends BaseService implements RecruitmentPCService {
    /**
     * 删除状态
     */
    private final static Integer DETF_STATUS=1;
    /**
     * 招聘状态
     */
    private final static Integer REC_FLAG=1;
    @Resource
    RecruitmentMapper recruitmentMapper;
    @Resource
    IntroduceJobMapper introduceJobMapper;


    /**
     * 按条件查询应聘信息
     * @param frontPC
     * @return
     */
    @Override
    public PCResult qryIntroducePc(RecruitmentFrontPC frontPC) {
        Example example = new Example(Recruitment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNotNull("userId");
        criteria.andEqualTo("flag", REC_FLAG);
        if(frontPC.getKind()!=null&&!"".equals(frontPC.getKind())){
            criteria.andEqualTo("kind",frontPC.getKind());
        }
        if(frontPC.getName()!=null&&!"".equals(frontPC.getName())){
            criteria.andLike("name","%"+frontPC.getName()+"%");
        }
       /* if(frontPC.getJobDuty()!=null&&!"".equals(frontPC.getJobDuty())){
            criteria.andLike("jobDuty","%"+frontPC.getJobDuty()+"%");
        }*/
        List<Recruitment> recruitments = recruitmentMapper.selectByExample(example);
        PCResult pcResult = new PCResult();
        if(recruitments!=null&&recruitments.size()>0){
            List<Integer> ids=new ArrayList<Integer>();
            for (Recruitment recruitment : recruitments) {
                Integer id = recruitment.getId();
                ids.add(id);
            }
            Page<RecruitmentBackPC> pages=null;
            if(frontPC.getIntroduceName()!=null&&!"".equals(frontPC.getIntroduceName())){
                PageHelper.startPage(frontPC.getPage(),frontPC.getPageSize());
                pages = (Page<RecruitmentBackPC>)introduceJobMapper.qryRecruitmentPC2(ids,frontPC.getIntroduceName());
            }else{
                PageHelper.startPage(frontPC.getPage(),frontPC.getPageSize());
                pages = (Page<RecruitmentBackPC>)introduceJobMapper.qryRecruitmentPC(ids);
            }

            pcResult.setRecruitmentBackPCS(pages.getResult());
            pcResult.setTotals(pages.getTotal());
        }
        return pcResult;
    }

    /**
     * 查询应聘岗位详细信息
     * @param id
     * @return
     */
    @Override
    public IntroducePC qryIntroducePcById(Integer id) {
        IntroducePC introducePC = introduceJobMapper.qryIntroducePcById(id);
        if(introducePC==null||"".equals(introducePC)){
            throw new BusinessRuntimeException(ResultCodeEnum.DET_INTRODUCE.getCode(),ResultCodeEnum.DET_INTRODUCE.getMessage());
        }
        return introducePC;
    }

    /**
     * 根据应聘id删除
     * @param id
     * @return
     */
    @Override
    public Integer deleteByIntroduceId(Integer id) {
        Example example = new Example(IntroduceJob.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        IntroduceJob introduceJob = new IntroduceJob();
        introduceJob.setRef(DETF_STATUS);
        int result = introduceJobMapper.updateByExampleSelective(introduceJob, example);
        return result;
    }

    /**
     * 应聘信息批量删除
     * @param ids
     * @return
     */
    @Override
    public Integer deleteIntroduceByList(List<Integer> ids) {
        Integer result = introduceJobMapper.deleteIntroduceByList(ids);
        return result;
    }

    /**
     * 根据条件查询招聘信息
     * @param frontPC
     * @return
     */
    @Override
    public RecruitmentPCResult qryRecruitmentListPC(RecFront frontPC) {
        PageHelper.startPage(frontPC.getPage(),frontPC.getPageSize());
        Page<RecBack> pages=(Page<RecBack>)recruitmentMapper.selectRecruitmentByRecFront(frontPC);
        RecruitmentPCResult recruitmentPCResult = new RecruitmentPCResult();
        recruitmentPCResult.setRecBacks(pages.getResult());
        recruitmentPCResult.setTotals(pages.getTotal());
        return recruitmentPCResult;
    }

    /**
     * 更新招聘信息
     * @param recruitment
     * @return
     */
    @Override
    public Integer updateRecruitment(Recruitment recruitment) {
        recruitment.setUpdateTime(new Date());
        recruitment.setFlag(REC_FLAG);
        return recruitmentMapper.updateByPrimaryKey(recruitment);
    }

    /**
     * 添加招聘信息
     * @param recruitment
     * @return
     */
    @Override
    public Integer insertRecruitment(Recruitment recruitment) {
        recruitment.setUpdateTime(new Date());
        recruitment.setFlag(REC_FLAG);
        return recruitmentMapper.insertSelective(recruitment);
    }

   /**
     * 删除招聘职位
     * @param id
     * @return
     */
	@Override
	public Integer deleteRecruitmentById(Integer id) {
		Recruitment record=new Recruitment();
		record.setId(id);
		//删除状态
		record.setFlag(2);
		return recruitmentMapper.updateByPrimaryKeySelective(record);	
	}
	
	/**
     * 批量删除职位
     * @param ids
     * @return
     */
	@Override
	public Integer deleteRecruitementByIds(List<Integer> ids) {
		return recruitmentMapper.deleteRecruitmentByList(ids);
	}

}
